﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace POS_NEW
{
    public partial class frmChiPhi_ChinhSua : Form
    {
        public frmChiPhi_ChinhSua()
        {
            InitializeComponent();
        }
        string output = "";
        int ID_ChiPhi = -1;
        #region "Load"
        private void LoadgridChiPhi()
        {
            gridChiPhi.DataSource = AccessDataLayer.ExecuteDT("spChiPhi_Select", CommandType.StoredProcedure);
            gridChiPhi.DisplayLayout.Bands[0].Columns["Mã chi phí"].Hidden = true;
            gridChiPhi.DisplayLayout.Bands[0].Override.CellClickAction = CellClickAction.RowSelect;
            gridChiPhi.DisplayLayout.PerformAutoResizeColumns(true, Infragistics.Win.UltraWinGrid.PerformAutoSizeType.VisibleRows);
            gridChiPhi.DisplayLayout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
        }
        #endregion
        #region "Them"
        private int ThemChiPhi(string Ten_ChiPhi, string MoTa)
        {
            object[] ob = { "@Ten_ChiPhi", Ten_ChiPhi, "@MoTa", MoTa };
            return AccessDataLayer.ExecuteNonQuery("spChiPhi_Insert", "@output", ref output, CommandType.StoredProcedure, ob);
        }
        #endregion
        #region "Xoa"
        private int XoaChiPhi(string ID_ChiPhi)
        {
            object[] ob = { "@ID_ChiPhi", ID_ChiPhi };
            return AccessDataLayer.ExecuteNonQuery("spChiPhi_Delete", CommandType.StoredProcedure, ob);
        }
        #endregion
     
        private void btnChiPhi_Luu_Click(object sender, EventArgs e)
        {
            if (txtChiPhi.Text.Trim() == "")
            {
                using (ErrorScreen ers = new ErrorScreen("Nhập đầy đủ thông tin !", "Thông báo", 3))
                    ers.ShowDialog();
                return;
            }

            int kq = -1;
            try
            {
                kq = ThemChiPhi(txtChiPhi.Text.Trim(), txtChiPhi_GhiChu.Text.Trim());
            }
            catch (Exception ex)
            {
                using (ErrorScreen ers = new ErrorScreen(ex.Message))
                    ers.ShowDialog();
                output = "";
                return;
            }
            if (output == "-2")
            {
                using (ErrorScreen ers = new ErrorScreen("Chi phí đã tồn tại vui lòng nhập lại !", "Thông báo", 3))
                    ers.ShowDialog();
                output = "";
                return;
            }
            else
                if (kq > 0)
                {
                    using (ErrorScreen ers = new ErrorScreen("Lưu thành công !", "Thông báo", 3))
                        ers.ShowDialog();
                    txtChiPhi.Enabled = false;
                    txtChiPhi_GhiChu.Enabled = false;
                    txtChiPhi.ResetText();
                    txtChiPhi_GhiChu.ResetText();
                    btnChiPhi_Luu.Enabled = false;
                    btnChiPhi_Them.Focus();
                    LoadgridChiPhi();
                    output = "";
                }
                else
                {
                    using (ErrorScreen ers = new ErrorScreen("Có lỗi xảy ra !", "Thông báo", 3))
                        ers.ShowDialog();
                    output = "";
                    return;
                }


        }

        private void btnChiPhi_Them_Click(object sender, EventArgs e)
        {
            txtChiPhi.Enabled = true;
            txtChiPhi_GhiChu.Enabled = true;
            txtChiPhi.ResetText();
            txtChiPhi_GhiChu.ResetText();
            btnChiPhi_Luu.Enabled = true;
            txtChiPhi.Focus();
            ID_ChiPhi = -1;
        }
        private void btnChiPhi_Xoa_Click(object sender, EventArgs e)
        {
            if (gridChiPhi.Rows.Count > 0)
                if (gridChiPhi.ActiveRow != null)
                {
                    if (ID_ChiPhi == -1)
                        return;
                    using (ConfirmScreen cfs = new ConfirmScreen("Tất cả các lịch sử  :  \n" + gridChiPhi.ActiveRow.Cells[1].Value.ToString()
                        + " sẽ bị xóa. \n Bạn có chắc chắn xóa ?"))
                    {
                        if (cfs.ShowDialog() == DialogResult.OK)
                        {
                            try
                            {
                                if (this.XoaChiPhi(gridChiPhi.ActiveRow.Cells[0].Value.ToString()) > 0)
                                {
                                    using (ErrorScreen er = new ErrorScreen("Đã xóa :" + gridChiPhi.ActiveRow.Cells[1].Value.ToString(), "Thông báo", 3))
                                    {
                                        er.ShowDialog();
                                        LoadgridChiPhi();
                                    }
                                }
                                else
                                {
                                    using (ErrorScreen ers = new ErrorScreen("Có lỗi xảy ra !", "Thông báo", 3))
                                        ers.ShowDialog();
                                    return;
                                }
                            }
                            catch (Exception ex)
                            {
                                using (ErrorScreen ee = new ErrorScreen(ex.Message))
                                {
                                    ee.ShowDialog();
                                }
                            }
                        }
                        else
                        {
                            gridChiPhi.DisplayLayout.Bands[0].Override.AllowDelete = DefaultableBoolean.False;
                        }
                    }
                }
        }

        private void frmChiPhi_ChinhSua_Load(object sender, EventArgs e)
        {
            LoadgridChiPhi();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtChiPhi_GhiChu_Enter(object sender, EventArgs e)
        {
            txtChiPhi_GhiChu.SelectAll();
        }

        private void txtChiPhi_Enter(object sender, EventArgs e)
        {
            txtChiPhi.SelectAll();
        }

        private void gridChiPhi_ClickCell(object sender, Infragistics.Win.UltraWinGrid.ClickCellEventArgs e)
        {
            if(gridChiPhi.Rows.Count>0)
            if (gridChiPhi.ActiveRow != null)
            {
                txtChiPhi.Enabled = false;
                txtChiPhi_GhiChu.Enabled = false;
                txtChiPhi.ResetText();
                txtChiPhi_GhiChu.ResetText();
                ID_ChiPhi = int.Parse(gridChiPhi.ActiveRow.Cells["Mã chi phí"].Text.ToString());
            }
        }

        private void txtChiPhi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtChiPhi_GhiChu.Focus();
            }
        }

        private void txtChiPhi_GhiChu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChiPhi_Luu.Focus();
            }
        }

        private void frmChiPhi_ChinhSua_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
                btnChiPhi_Them_Click(null, null);
            else
                if (e.KeyCode == Keys.F4)
                {
                    if(btnChiPhi_Luu.Enabled)
                    btnChiPhi_Luu_Click(null, null);
                }
                else
                    if (e.KeyCode == Keys.Delete)
                        btnChiPhi_Xoa_Click(null, null);
                    else
                        if (e.KeyCode == Keys.Escape)
                            btnThoat_Click(null, null);
        }

        private void gridChiPhi_AfterSelectChange(object sender, AfterSelectChangeEventArgs e)
        {
            gridChiPhi_ClickCell(null, null);
        }
    }
}
